package com.housediyadmin.view.components
{
	/**
	 * 
	 * @author Bo Zhang
	 * 2011/01/07
	 */	
	import com.adobe.cairngorm.popup.PopUpEvent;
	import com.adobe.cairngorm.popup.PopUpWrapper;
	
	import com.housediyadmin.model.GoodsModel;
	import com.housediyadmin.model.GoodsTreeModel;
	import com.housediyadmin.model.NewsModel;
	import com.housediyadmin.events.goodsEvents.GetGoodsByNewsIDEvent;
	import com.housediyadmin.events.goodsEvents.GetGoodsByTypeIDEvent;
	import com.housediyadmin.events.newsEvents.DeleteNewsEvent;
	import com.housediyadmin.events.newsEvents.AddNewsEvent;
	import com.housediyadmin.events.newsEvents.EditNewsEvent;
	import com.housediyadmin.events.newsEvents.GetNewsEvent;
	import com.housediyadmin.events.treeEvents.GetGoodsTreeEvent;
	import com.housediyadmin.events.RemoveGoodsEvent;
	import com.housediyadmin.vo.Goods;
	import com.housediyadmin.vo.News;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.core.DragSource;
	import mx.core.IUIComponent;
	import mx.events.DragEvent;
	import mx.events.CalendarLayoutChangeEvent;
	import mx.events.CalendarLayoutChangeEvent;
	import mx.events.CloseEvent;	
	import mx.events.ListEvent;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.DateField;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	import mx.managers.DragManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.Label;
	import spark.components.List;
	
	public class NewsPopUpMediator extends Mediator
	{
		[Inject]
		public var view:NewsPopUp;
		
		[Inject]
		public var model:GoodsModel;
		
		[Inject]
		public var newsModel:NewsModel;
		
		[Inject]
		public var goodsTreeModel:GoodsTreeModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
			
		override public function onRegister():void
		{
			var dateFormatter:DateFormatter = new DateFormatter();
			dateFormatter.formatString = "YYYY年MM月DD日";
			
			BindingUtils.bindProperty(view.goodsTree, "dataProvider", goodsTreeModel, "goodsTreeXML");
			dispatch(new GetGoodsTreeEvent(GetGoodsTreeEvent.GET_GOODS_TREE));
			view.goodsTree.addEventListener(ListEvent.ITEM_CLICK, selectTreeNode);
			
			BindingUtils.bindProperty(view.allGoodslist, "dataProvider", goodsModel, "goodsArray");
			BindingUtils.bindProperty(view.selectedGoodsList,"dataProvider", goodsModel, "goodsNewsArray");
			
			if (view.currentState == "add")
			{
				goodsModel.goodsNewsArray = new ArrayCollection();		
				goodsModel.goodsArray = new ArrayCollection();
				// text
				view.newsCreateTime.text = dateFormatter.format(new Date());
				view.newsEditorTime.text = dateFormatter.format(new Date());
				view.newsCreateTime.enabled = false;
				view.newsEditorTime.enabled = false;
				// btn
				view.okBtn.addEventListener(MouseEvent.CLICK, addNewsEve);
			}
			else if (view.currentState == "edit")
			{
				goodsModel.goodsArray = new ArrayCollection();
				BindingUtils.bindProperty(view.newsTitle, "text", newsModel.selectedNews, "newsTitle");
				BindingUtils.bindProperty(view.newsContent, "text", newsModel.selectedNews, "newsContent");
				BindingUtils.bindProperty(view.newsSource, "text", newsModel.selectedNews, "newsSource");
				BindingUtils.bindProperty(view.newsCreator, "text", newsModel.selectedNews, "newsCreator");
				BindingUtils.bindProperty(view.newsEditor, "text", newsModel.selectedNews, "newsEditor");
				
				var date:Date = newsModel.selectedNews.newsCreateDate;
				view.newsCreateTime.text = dateFormatter.format(date);
				view.newsEditorTime.text = dateFormatter.format(newsModel.selectedNews.newsEditDate);
				view.goodsTree.addEventListener(ListEvent.ITEM_CLICK, selectTreeNode);
				view.newsCreateTime.enabled = false;
				view.newsEditorTime.enabled = false;
				view.newsCreator.enabled = false;	
				dispatch(new GetGoodsByNewsIDEvent(GetGoodsByNewsIDEvent.GET_GOODS_BY_NEWSID, newsModel.selectedNews.newsID));
				// btn
				view.okBtn.addEventListener(MouseEvent.CLICK, modifyNewsEve);
			}
			view.selectedGoodsList.dataProvider = model.goodsNewsArray;
			
			view.allGoodslist.addEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);			
			view.addEventListener(RemoveGoodsEvent.REMOVE_GOODS, removeGoodsHandler);
			view.selectedGoodsList.addEventListener(DragEvent.DRAG_ENTER, dragEnterHandler);		
			view.selectedGoodsList.addEventListener(DragEvent.DRAG_DROP, dragDropHandler);	
			view.cancelBtn.addEventListener(MouseEvent.CLICK, cancelEve);	
		}
		
		private function startDragHandler(event:MouseEvent):void
		{
			if (view.allGoodslist.selectedItem != null)
			{
				var ds:DragSource = new DragSource();
				ds.addData((view.allGoodslist.selectedItem as Goods), "moveGoods");
				
				var imageProxy:Label = new Label();
				imageProxy.text = (view.allGoodslist.selectedItem as Goods).goodsName;
				
				DragManager.doDrag(view.allGoodslist, ds, event, imageProxy);
			}
			else
			{
				Alert.show("请选择需要添加的商品");
			}
			
		}
			
		private function removeGoodsHandler(event:RemoveGoodsEvent):void
		{
			model.goodsNewsArray.removeItemAt(model.goodsNewsArray.getItemIndex(event.goods));
		}
			
		private function dragEnterHandler(event:DragEvent):void
		{
			var selectedGoods:Goods = event.dragSource.dataForFormat(event.dragSource.formats[0]) as Goods;		
			var couldDrop:Boolean = true;
			
			for each (var goods:Goods in model.goodsNewsArray)
			{
				if (goods.goodsName == selectedGoods.goodsName)
				{
					couldDrop = false;
					break;	
				}
			}
			
			if (couldDrop == true && event.dragSource.hasFormat("moveGoods"))
			{
				DragManager.acceptDragDrop(IUIComponent(event.target));
			}	
		}
		
		private function dragDropHandler(event:DragEvent):void
		{
			model.goodsNewsArray.addItem(event.dragSource.dataForFormat(event.dragSource.formats[0]) as Goods);
		}
		
		private function addNewsEve(event:MouseEvent):void
		{
			var news:News = new News();	
			news.newsContent = view.newsContent.text;
			news.newsCreator = view.newsCreator.text;
			news.newsEditor = view.newsEditor.text;
			news.newsSource = view.newsSource.text;
			news.newsTitle = view.newsTitle.text;	
			news.newsCreateDate = new Date();
			news.newsEditDate = new Date();
			
			var goodsIds:Array = new Array();	
			for(var i:int = 0; i < goodsModel.goodsNewsArray.length; i++)
			{
				goodsIds.push((goodsModel.goodsNewsArray[i] as Goods).goodsID);
			}
			dispatch(new AddNewsEvent(AddNewsEvent.ADD_NEWS, news, goodsIds));
			
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
			newsModel.selectedNews = new News();
		}
		
		private function modifyNewsEve(event:MouseEvent):void
		{		
			newsModel.selectedNews.newsContent = view.newsContent.text;
			newsModel.selectedNews.newsTitle = view.newsTitle.text;
			newsModel.selectedNews.newsSource = view.newsSource.text;
			newsModel.selectedNews.newsCreator = newsModel.selectedNews.newsCreator;
			newsModel.selectedNews.newsEditor = view.newsEditor.text;
			newsModel.selectedNews.newsCreateDate = newsModel.selectedNews.newsCreateDate;
			newsModel.selectedNews.newsEditDate = new Date();
			
			var goodsIds:Array = new Array();	
			for(var i:int; i < goodsModel.goodsNewsArray.length; i ++)
			{
				goodsIds.push((goodsModel.goodsNewsArray[i] as Goods).goodsID);
			}
			dispatch(new EditNewsEvent(EditNewsEvent.EDIT_NEWS, newsModel.selectedNews, goodsIds));
			
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
			newsModel.selectedNews = new News();
		}
		
		private function cancelEve(event:MouseEvent):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
			newsModel.selectedNews = new News();
		}
 		
		private function selectTreeNode(event:ListEvent):void
		{
			goodsTreeModel.selectedNode = event.target.selectedItem as XML;	
			
			var goodsIDs:Array = new Array;	
			var xmlList:XMLList = event.target.selectedItem.@typeID;
			
			if(xmlList.length()!=0)
			{
				for each(var xml:XML in xmlList)
				{
					goodsIDs.push((int)(xml.toXMLString()));
				}
				dispatch(new GetGoodsByTypeIDEvent(GetGoodsByTypeIDEvent.GET_GOODS_BY_TYPEID, goodsIDs));
			}
			
		}
	}
}